package com.robosense.datacenter.dynamic.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

/**
 * 对指定文件夹下Java类中中方法进行切面拦截
 * 获取方法上的信息，进行逻辑处理
 */
@Component
@Aspect
@Order(-101)
@Slf4j
public class MethodAspect {

    @Pointcut("execution(* com.robosense.datacenter.controller..*.*(..))")
    private void method() {
    }

    @Before("method()")
    public void method(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        MethodTimes methodTimes = methodSignature.getMethod().getAnnotation(MethodTimes.class);
        if (!Objects.isNull(methodTimes)) {
            Object[] args = joinPoint.getArgs();
            Arrays.asList(args).forEach(System.out::println);
            log.info("切面======统计方法被调用次数=====");
            log.error(methodTimes.value() + "==" + methodSignature.getMethod());
        }
    }


}
